package org.example.cotroller.wechat;


import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.*;
import org.example.service.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/wechat")
public class WeChatApi {

    @Resource
    private AdService adService;

    @Resource
    private GoodsService goodsService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private UserService userService;
    @Resource
    private AddressService addressService;

    @Resource
    private OrderUserService orderUserService;

    @Resource
    private CommentService commentService;
    /**
     * 微信小程序接口
     * */


    /**
     * 首页轮播图
     * */
    @GetMapping("/adList")
    public Result<List<Ad>> getAdList() {
        log.info("首页轮播图");

        return Result.success(adService.getAdList());

    }

    /**
     * 商品列表
     * */

    @GetMapping("/goodsList")
    public Result<List<Goods>> getGoodsList() {
        log.info("商品列表");

        return Result.success(goodsService.goodsList());
    }
    /**
     * 分类界面*/
    @GetMapping("/categoryList")
    public Result<List<GoodsByCategory>> getCategoryList() {
        log.info("分类界面");
        List<GoodsByCategory> goodsByCategoryList;
        List<Category> list = categoryService.allCategoryList();
        list.add(0,new Category("推荐"));
        goodsByCategoryList = goodsService.getGoodsByCategory(list);

        return Result.success(goodsByCategoryList);
    }

    /**
     * 新增收货地址
     * */

    @PostMapping("/address")
    public Result addAddress(@RequestBody Address address){
        log.info("新增收货地址:{}",address.toString());
        if (addressService.addAddress(address))
            return Result.success();
        else return Result.error("添加失败");

    }

    /**
     * 删除收货地址
     * */
    @GetMapping("/address/{addressId}")
    public Result deleteAddress(@PathVariable Integer addressId){
        log.info("删除收货地址:{}",addressId);
        if (addressService.deleteAddress(addressId))
            return Result.success();
        else return Result.error("指定id不存在或者其他错误");
    }

    /**
     * 修改收货地址信息
     * */
    @PostMapping("/address/{addressId}")
    public Result updateAddress(@RequestBody Address address){
        log.info("修改收货地址信息:{}",address.toString());

        if (addressService.updateAddress(address))
            return Result.success();
        else return Result.error("错误");
    }

    @GetMapping("/address/list")
    public Result<List<Address>> getAddressList(@RequestParam(value = "openId",required = true) String openId){
        log.info("获取所有地址:{}",openId);
        return Result.success(addressService.list(openId));
    }
    /**
     * 微信登录接口
     * */
    @PostMapping("/login")
    public Result<List<String>> weChatLogin(@RequestBody String code){
        log.info("微信登录接口");
        JSONObject jsonObject = JSONObject.parseObject(code);
        code = jsonObject.getString("code");
        System.out.println(code);
        return Result.success(userService.login(code));

    }

    /**
     * 获取默认地址
     * */

    @GetMapping("/address/default")
    public Result<Address> getDefaultAddress(String openId){
        log.info("获取默认地址");
        return Result.success(addressService.getDefaultAddress(openId));
    }

    /**
     * 新增订单
     **/
    @PostMapping("/order")
    public Result getOder(@RequestBody OrderUser orderUser){
        log.info("新增订单：:{}",orderUser.toString());
        if (orderUserService.addOrderUser(orderUser))
            return Result.success();
        else return Result.error("添加失败");
    }


    /**
     * 用户获取自己全部订单
     * */

    @GetMapping("/order/list")
    public Result<OrderUserPageByStatus> getOrderList(@RequestParam(name = "openId") String openId){
        log.info("用户获取自己全部订单:{}",openId);
        return Result.success(orderUserService.getOrderUserByOpenId(openId));
    }

    /**
     * 用户取消订单
     * */
    @GetMapping("/order/cancel")
    public Result cancelOrder(@RequestParam(name = "orderId") String orderId){
        log.info("取消订单：{}",orderId);
        if (orderUserService.cancelOrder(orderId))
            return Result.success();
        else return Result.error("订单已发货");
    }

    /**
     * 用户完成订单收货
     * */
    @GetMapping("/order/complete")
    public Result orderComplete(@RequestParam(name = "orderId") String orderId){
        log.info("完成订单：{}",orderId);
        if (orderUserService.orderComplete(orderId))
            return Result.success();
        else return Result.error("订单不存在或其他问题");
    }
    /**
     * 用户对菜品评论
     * */
    @PostMapping("/order/comment")
    public Result orderComment(@RequestBody String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        String openId = jsonObject.getString("openId");
        Integer orderId = jsonObject.getInteger("orderId");
        String commentText = jsonObject.getString("commentText");
        log.info("添加评论至订单：{},内容：{},用户id：{}",orderId,commentText,openId);
        if (commentService.addComment(openId,orderId,commentText))
            return Result.success();
        else return Result.error("添加失败");
    }

    /**
     * 删除评论
     * */
    @GetMapping("/order/comment")
    public Result deleteComment(@RequestParam(name = "commentId") Integer commentId){
        log.info("删除id：{}",commentId);
        if (commentService.deleteComment(commentId))
            return Result.success();
        else return Result.error("删除失败");
    }

    /**
     * 用户获取自己评论列表
     * */
    @GetMapping("/order/comment/user")
    public Result<List<Comment>> getUsersComment(@RequestParam(name = "openId") String openId){
        log.info("查询用户id:{}的评论",openId);
        return Result.success(commentService.getUsersComment(openId));
    }

    /**
     * 菜品评论列表
     * */
    @GetMapping("/order/comment/goods")
    public Result<List<Comment>> getUsersComment(@RequestParam(name = "goodsId") Integer goodsId){
        log.info("查询菜品id:{}的评论",goodsId);
        return Result.success(commentService.getGoodsComment(goodsId));
    }


}
